home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Examples / AppKit / Lines / LinesView.m < prev    next >
Encoding:
Text File  |  1997-01-13  |  8.4 KB  |  210 lines

  1. /*
  2.  * LinesView.m, a small sample view for showing timer & userpaths.
  3.  * Author: Ali T. Ozer, NeXT Computer, Inc.
  4.  * Written March '89.
  5.  * Updated for 2.0 Oct '90 by Jayson Adams to use UserPath.[hm].
  6.  * Updated for 3.0 March '92 by Ali Ozer
  7.  * Converted to OpenStep and updated for 4.2 Dec '96, Trey Matteson
  8.  *
  9.  * You may freely copy, distribute, and re-use the code in this example. NeXT
  10.  * disclaims any warranty of any kind, expressed or implied, as to its fitness
  11.  * for any particular purpose.
  12.  *
  13.  * LinesView draws a number of connected lines whose endpoints bounce around
  14.  * randomly within the bounds of the view. The endpoints are stored in
  15.  * an data array which is passed to PostScript as a user path. The
  16.  * animation is performed by calling the "animate" method as often as
  17.  * possible through a timer.
  18.  */
  19.  
  20. #import <AppKit/AppKit.h>
  21. #import <stdlib.h>                    // For rand(), srand().
  22. #import "LinesView.h"
  23.  
  24. #define RANDINT(n) (rand() % (n+1))    // Return random integer 0..n
  25. #define INITRAND   srand((unsigned)(fabs(fmod([NSDate timeIntervalSinceReferenceDate], (double)INT_MAX))))
  26.  
  27. #define XVEL corners[count].xVel  // Some slimy shortcuts, asuuming we're
  28. #define YVEL corners[count].yVel  // using "count" as corner counter.
  29. #define XLOC corners[count].xLoc
  30. #define YLOC corners[count].yLoc
  31.  
  32. #define MAXVEL 12        // Maximum velocity of corners (pixels per frame)
  33.  
  34.  
  35. @implementation LinesView
  36.  
  37. /* Create the view and related stuff...
  38. */
  39. - (id)initWithFrame:(NSRect)rect {
  40.     [super initWithFrame:rect];
  41.  
  42.     /* Allocating a gstate is an easy way to speed up lockFocus/unlockFocus, and, hence, drawing... */
  43.     [self allocateGState];
  44.  
  45.     /* Initialize the random number generator... */
  46.     INITRAND;
  47.  
  48.     /* Listen to interesting notifications */
  49.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillHide:) name:NSApplicationWillHideNotification object:[NSApplication sharedApplication]];
  50.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillMiniaturize:) name:NSWindowWillMiniaturizeNotification object:nil];
  51.  
  52.     /* create a user path */
  53.     userPath = [[UserPath allocWithZone:[self zone]] init];
  54.     running = NO;
  55.  
  56.     return self;
  57. }
  58.  
  59. - (void)dealloc {
  60.     /* Stop the timer */
  61.     if (running) {
  62.     [linesTimer invalidate];
  63.         [linesTimer release];
  64.     }
  65.     /* Remove self as observer */
  66.     [[NSNotificationCenter defaultCenter] removeObserver:self];
  67.     [userPath release];
  68.     [super dealloc];
  69. }
  70.  
  71. - (void)toggleRun:(id)sender {
  72.     /* start or stop the timer (we're called by a two-state button) */
  73.     if (running) {
  74.         [linesTimer invalidate];
  75.         [linesTimer release];
  76.     running = NO;
  77.     } else {
  78.       /* Call the animation method as often as possible... */
  79.     linesTimer = [[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(animate:) userInfo:nil repeats:YES] retain];
  80.     running = YES;
  81.     } 
  82. }
  83.  
  84. /* This method should be connected to a UI object capable of generating numbers. Note that to successfully detect the initial value of this slider as set through IB, we also declare an outlet named "numberOfCorners," and connect it to this UI object. Thus this method (setNumberOfCorners:) gets called when the .nib is being loaded, and we can detect the initial value of the slider.
  85. */
  86. - (void)setNumberOfCorners:(id)sender {
  87.     int       count;
  88.     int    oldNumCorners = numCorners;
  89.     NSRect bounds = [self bounds];
  90.     
  91.     /* set the number of corners based on the "corners" slider */
  92.     numCorners = MIN(MAXNUMCORNERS, MAX([sender intValue], MINNUMCORNERS));
  93.  
  94.     /* set the new corner starting positions & velocities */
  95.     for (count = oldNumCorners; count < numCorners; count++) {
  96.         XLOC = (int)(bounds.size.width / 4 + RANDINT((int)(bounds.size.width) / 2));
  97.         YLOC = (int)(bounds.size.height / 4 + RANDINT((int)(bounds.size.height) / 2));
  98.         XVEL = (RANDINT(1) ? 1 : -1) * (1 + RANDINT(MAXVEL/2));
  99.         YVEL = (RANDINT(1) ? 1 : -1) * (1 + RANDINT(MAXVEL/2));
  100.     }
  101.     userPathNeedsUpdating = YES;
  102.     [self setNeedsDisplay:YES]; 
  103. }
  104.  
  105. /* drawRect: is supposed to regenerate the image of the whole view (or at least the specified rect); it does this by clearing the background then redrawing the lines on top...
  106. */
  107. - (void)drawRect:(NSRect)rects {
  108.     /* fill with the background color */
  109.     [[self backgroundColor] set];
  110.     NSRectFill ([self bounds]);
  111.     /* draw the lines */
  112.     [self drawUserPath:[self lineColor]];
  113. }
  114.  
  115. /* Draws the lines by sending the user path down... If the user path is out of date, updates it by reloading all the endpoints in.
  116. */
  117. - (void)drawUserPath:(NSColor *)color {
  118.     /* Load all the endpoints into the user path object... */
  119.     if (userPathNeedsUpdating) {
  120.         unsigned count;
  121.         /* "plot" the points */
  122.         [userPath beginUserPath:NO];
  123.         for (count = 0; count < numCorners; count++) {
  124.             if (count) {
  125.                 [userPath lineto:XLOC :YLOC];
  126.             } else {
  127.                 [userPath moveto:XLOC :YLOC];
  128.             }
  129.         }
  130.         [userPath closepath];
  131.         [userPath endUserPath:dps_ustroke];
  132.         userPathNeedsUpdating = NO;
  133.     }
  134.     /* Draw the user path */
  135.     [color set];
  136.     PSsetlinewidth(0.0);
  137.     [userPath sendUserPath];
  138. }
  139.  
  140. /* Lines is an unusual animation program in that it runs untimed; that is, it runs as fast as the CPU will allow, and it doesn't care that on faster CPUs the animation will run faster. An animation or game application will usually want to limit to frame rate to a value (for instance, 30 frames a second), and on hardware not capable of that rate, end up doing the best it can. Such an application would also look at the time that actually passed between frames and increment the animation or game play accordingly.
  141.  
  142. Lines accomplishes its goal of running as fast as possible by creating a timer with a 0.0 second period. This means that the timer will fire and this method (animate) will be called as soon as possible.
  143.  
  144. Lines uses a buffered output window as a means to fake double-buffered animation. The current frame is drawn directly into the window. However, because the window is buffered, the drawing goes to the backing store, and not the screen.  Only when the frame is complete does Lines flush the window contents to the screen; this process is fast and provides a flicker-free update.  The next frame is then drawn into the backing store, and the cycle continues.
  145.  
  146. Note that this method does its own lock/unlockFocus, and flushWindow. Again, this is done for efficiency, and apps often don't need to go to those lengths to draw. This method could have called setNeedsDisplay:YES, but that would have caused drawRect: to be eventually called. drawRect: does more complete drawing (including erasing the background), and we don't really need that in this case. We know what to erase from the previous frame, so we do the erase and redraw ourselves with an explicit lock/unlockFocus.
  147. */
  148. - (void)animate:(NSTimer *)timer {
  149.     int count;
  150.     NSRect bounds = [self bounds];
  151.     
  152.     [self lockFocus];
  153.  
  154.     /* Clear the previous frame from the backing store. Note that in some cases it might
  155.        make more sense to just clear the whole view with an NSRectFill(). */
  156.     [self drawUserPath:[self backgroundColor]];
  157.  
  158.     /* Move all the corners... */
  159.     for (count = 0; count < numCorners; count++) {
  160.         XLOC += XVEL;
  161.         YLOC += YVEL;
  162.  
  163.     /* Detect collision with sides; if we collide, bounce back in some random fashion. */
  164.         if (XLOC >= bounds.size.width) {
  165.             XLOC = bounds.size.width-1;
  166.             XVEL = -1-RANDINT(MAXVEL);
  167.         } else if (XLOC < bounds.origin.x) {
  168.             XLOC = bounds.origin.x;
  169.             XVEL = 1+RANDINT(MAXVEL);
  170.         }
  171.         if (YLOC >= bounds.size.height) {
  172.             YLOC = bounds.size.height-1;
  173.             YVEL = -1-RANDINT(MAXVEL);
  174.         } else if (YLOC < bounds.origin.y) {
  175.             YLOC = bounds.origin.y;
  176.             YVEL = 1+RANDINT(MAXVEL);
  177.         }
  178.     }
  179.     userPathNeedsUpdating = YES;
  180.  
  181.     /* Draw the new frame */
  182.     [self drawUserPath:[self lineColor]];
  183.  
  184.     /* And flush the backing store to the window */
  185.     [[self window] flushWindow];
  186.     [self unlockFocus]; 
  187. }
  188.  
  189. - (NSColor *)lineColor {
  190.     return [NSColor blackColor];
  191. }
  192.  
  193. - (NSColor *)backgroundColor {
  194.     return [NSColor whiteColor];
  195. }
  196.  
  197. - (void)windowWillMiniaturize:(NSNotification *)notification {
  198.     if (running && ([notification object] == [self window])) {
  199.     [self toggleRun:nil];
  200.     }
  201. }
  202.  
  203. - (void)appWillHide:(NSNotification *)notification {
  204.     if (running) {
  205.     [self toggleRun:nil];
  206.     }
  207. }
  208.  
  209. @end
  210.